<?php
namespace App\Api\Fcjd;

use PhalApi\Api;

/**
 * 批量设置接口
 */
class Setting extends Api {
    
    public function getRules() {
        return array(
            'updateSettings' => array(
                'ids' => array('name' => 'ids', 'type' => 'array', 'require' => true, 'desc' => '需要更新的ID数组'),
                'tasks' => array('name' => 'tasks', 'type' => 'array', 'require' => true, 'desc' => '要追加的任务数组'),
                'run_status' => array('name' => 'run_status', 'require' => true, 'desc' => '运行状态'),
                'payment_method' => array('name' => 'payment_method', 'require' => true, 'desc' => '提现方式'),
            ),
        );
    }

    /**
     * 批量更新设置
     * 
     * @desc 根据传入的ids数组和tasks数组追加任务设置
     * @return array
     */
    public function updateSettings() {
        $ids = $this->ids;
        $newTasks = $this->tasks;
        $run_status = $this->run_status;
        $payment_method = $this->payment_method;

        if($payment_method=='支付宝'){
            $payment_method="小额支付宝自动提现";
        }
        $withdrawTypeJson=array(
            "withdrawType" => $payment_method
        );
        $payment_method_json = json_encode($withdrawTypeJson,JSON_UNESCAPED_UNICODE);
        
        // 获取所有指定ID的记录当前的task值
        $records = \PhalApi\DI()->notorm->phone_list
            ->select('*')
            ->where(array('id' => $ids))
            ->fetchAll();

        $updateCount = 0;

        // return $records;
        foreach ($records as $record) {
            // 获取现有的任务数组
            $currentTasks = !empty($record['task']) ? explode(',', $record['task']) : array();
            
            if($run_status == '运行'){
                // 合并现有任务和新任务，并去重
                $mergedTasks = array_unique(array_merge($currentTasks, $newTasks));
            }elseif($run_status == '停止'){
                // 从现有任务中移除指定的任务
                $mergedTasks = array_diff($currentTasks, $newTasks);
            }
            // 转回字符串格式
            $taskString = implode(',', $mergedTasks);
            // 更新单条记录
            $result = \PhalApi\DI()->notorm->phone_list
                ->where('id', $record['id'])
                ->update(array('task' => $taskString));

            // 更新 app_info 表 按设备手机号, 更新选择的脚本
            foreach($newTasks as $task){
                $App = \PhalApi\DI()->notorm->phone_app
                    ->where('id', $task)
                    ->fetchOne();
                
                //查询 app_info 表中是否存在该记录
                $info = \PhalApi\DI()->notorm->phone_app_info->where( array('mobile' => $record['mobile'], 'app_name' => $App['name'] ))->fetchOne();
                if($info){
                    
                    \PhalApi\DI()->notorm->phone_app_info->where( array('mobile' => $record['mobile'], 'app_name' => $App['name'] ))->update(
                        array(
                            'config' => $payment_method_json,
                            'update_time' => date("Y-m-d H:i:s", time())
                        )
                    );
                }else{
                    \PhalApi\DI()->notorm->phone_app_info->insert(
                        array(
                            'mobile' => $record['mobile'], 
                            'app_name' => $App['name'],
                            'name' => $record['name'],
                            'username' => $record['username'],
                            'oaid' => $record['oaid'],
                            'config' => $payment_method_json,
                            'create_time' => date("Y-m-d H:i:s", time())
                        )
                    );
                }
            }


            if ($result) {
                $updateCount++;
            }
        }

        \PhalApi\DI()->response->setMsg('设置成功');
        return array(
            'affected_rows' => $updateCount,
            'updated_ids' => $ids,
            'appended_tasks' => $newTasks
        );
    }
}
